﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;

namespace Prodma.DataAccess.DataService
{
    public class SurveyService : ISurveyRepository
    {

        private DataContext context = new DataContext();

        public string Options { get; set; }

        #region IRecordOperation<tb_Survey,SuveyVm> Members

        public IList<SurveyVm> GetList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from survey in context.tb_Survey
                            select new SurveyVm()
                            {
                                Id = survey.Id,
                                Name = survey.Name,
                                Question = survey.Question,
                                Project = new ProjectVm() { Id = survey.tb_Project.Id, Name = survey.tb_Project.Name },
                                State = new StateVm() { Id = survey.tb_State.Id, Name = survey.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        public void New(tb_Survey entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Survey(entity);
                context.SaveChanges();

                #region Add Options
                tb_Survey lastAddedSurvey = context.tb_Survey.OrderByDescending(x => x.Id).FirstOrDefault();

                if (Options != "")
                {
                    string[] selectedOptions = Options.Split(',');
                    foreach (string currentOption in selectedOptions)
                    {
                        tb_SurveyOption option = new tb_SurveyOption();
                        option.Name = currentOption;
                        option.tb_SurveyReference.EntityKey = new EntityKey("DataContext.tb_Survey", "Id", lastAddedSurvey.Id);

                        context.AddTotb_SurveyOption(option);
                        context.SaveChanges();
                    }
                }
            }
            #endregion
        }

        public SurveyVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from survey in context.tb_Survey
                            where survey.Id == id
                            select new SurveyVm()
                            {
                                Id = survey.Id,
                                Name = survey.Name,
                                Question = survey.Question,
                                Project = new ProjectVm() { Id = survey.tb_Project.Id, Name = survey.tb_Project.Name },
                                State = new StateVm() { Id = survey.tb_State.Id }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Survey entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from survey in context.tb_Survey
                            where survey.Id == id
                            select survey).FirstOrDefault();

                edit.Name = entity.Name;
                edit.Question = entity.Question;
                edit.tb_ProjectReference.EntityKey = entity.tb_ProjectReference.EntityKey;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from survey in context.tb_Survey
                              where survey.Id == id
                              select survey).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IFindOperation<SuveyVm> Members

        public IList<SurveyVm> GetList(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from survey in context.tb_Survey
                            where survey.Name.StartsWith(findName)
                            select new SurveyVm()
                            {
                                Id = survey.Id,
                                Name = survey.Name,
                                Question = survey.Question,
                                Project = new ProjectVm() { Id = survey.tb_Project.Id, Name = survey.tb_Project.Name },
                                State = new StateVm() { Id = survey.tb_State.Id, Name = survey.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from survey in context.tb_Survey
                             where survey.Id == id
                             select survey).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from survey in context.tb_Survey
                             where survey.Id == id
                             select survey).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion



        #region ISurveyRepository Members

        public void AddOption(tb_SurveyOption option)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_SurveyOption(option);
                context.SaveChanges();
            }
        }

        public void RemoveOption(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from option in context.tb_SurveyOption
                              where option.Id == id
                              select option).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

    }
}
